//
//  CaogaologViewController.m
//  YiqiMobile
//
//  Created by henry on 13-4-21.
//  Copyright (c) 2013年 henry. All rights reserved.
//

#import "CaogaologViewController.h"
#import "CaogaologViewCell.h"
#import "Utility.h"
#import "Config.h"
#import "JSON.h"
#import "DefineAction.h"
#import "AppDelegate.h"
#import "MBProgressHUD.h"
@interface CaogaologViewController ()

@end

@implementation CaogaologViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
       
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
     self.isEndData=NO;
    float height=379;
    if (iPhone5) {
        height+=88;
    }
    self.view.frame=CGRectMake(0, 0, 320, height);
    self.mytableView=[[UITableView alloc] initWithFrame:self.view.frame];
    self.mytableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    self.mytableView.dataSource=self;
    self.mytableView.delegate=self;
    [self.view addSubview:self.mytableView];
    self.arrayList=[NSMutableArray array];
    [self initFootLoadingMore];
    // Do any additional setup after loading the view from its nib.
}

-(void) initFootLoadingMore{
    UIView *footView=[[UIView alloc] initWithFrame:CGRectMake(0,  0, self.view.frame.size.width, 30)];
    footView.tag=FOOTVIEWTAG;
    footView.backgroundColor=[UIColor  clearColor];
    UIActivityIndicatorView *activityIndicatorView=[[UIActivityIndicatorView alloc]
                                                    initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    activityIndicatorView.frame=CGRectMake(100, 5, 20, 20);
    [activityIndicatorView startAnimating];
    
    [footView addSubview:activityIndicatorView];
    // [activityIndicatorView release];
    UILabel *moreLabel=[[UILabel alloc] initWithFrame:CGRectMake(135, 5, 110, 20)];
    moreLabel.backgroundColor=[UIColor clearColor];
    moreLabel.textColor=[UIColor blackColor];
    moreLabel.font=[UIFont fontWithName:FONT_FAMILY size:12.0f];
    moreLabel.text=LOADING_TITLE_DEFAULT;
    [footView addSubview:moreLabel];
    self.mytableView.tableFooterView=footView;
    self.mytableView.tableFooterView.hidden=YES;
    
}

-(void) loaddataFromserverWIthurrpage:(int)currpage{
    AppDelegate *app=(AppDelegate*)[[UIApplication sharedApplication] delegate];
    if (app.status==NotReachable) {
        [Utility customAlertWithMsg:NETWORTERROE andTitle:ALRRTTITLE andDelagate:nil];
        return;
    }
    NSMutableDictionary *param=[NSMutableDictionary dictionary];
    param[@"id"]=self.caogaoId;
    param[@"type"]=@(currpage);
    NSString *url=[NSString stringWithFormat:@"%@/%@",[Utility  getServerUrl],CAOGAOLOGLISTACTION];
    
    self.request=[[URLRequest alloc] initWithPostURL:url andDelegate:self andPostData:param andFile:nil];
      [[MBProgressHUD showHUDAddedTo:self.view animated:YES] setLabelText:LOADING_TITLE_DEFAULT];
    [self.request startLoadData];
}


- (void)requestFinished:(id)context{
    self.mytableView.tableFooterView.hidden=YES;
    self.mytableView.contentInset=UIEdgeInsetsMake(0, 0,0, 0);
    [MBProgressHUD hideAllHUDsForView:self.view animated:NO];
    @try{
        NSDictionary *result=[context JSONValue];
        if (result!=nil&&[[result  objectForKey:@"code"] integerValue]==0) {
            if ([[result objectForKey:@"method"] isEqualToString:CAOGAOLOGLISTACTION]) {
                NSArray *data=[result objectForKey:@"list"];
                if (data&&(NSNull*)data!=[NSNull null]&&data.count>0) {
                    if (self.currpage<=1) {
                        [self.arrayList removeAllObjects];
                    }
                     self.currpage=1;
                    [self.arrayList addObjectsFromArray:data];
                    self.isEndData=NO;
                    [self.mytableView  reloadData];
                }else{
                    self.isEndData=YES;
                }
            }
            
        }else{
            [Utility customAlertWithMsg:PROCESSERROE andTitle:ALRRTTITLE andDelagate:nil];
        }
    }@catch (NSException *e) {
        [Utility customAlertWithMsg:NETWORTERROE andTitle:ALRRTTITLE andDelagate:nil];
    }
}

- (void)requestFailed:(id)context error:(NSString *)error{
    self.mytableView.tableFooterView.hidden=YES;
    self.mytableView.contentInset=UIEdgeInsetsMake(0, 0,0, 0);
    [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:NO];
    [Utility customAlertWithMsg:NETWORTERROE andTitle:ALRRTTITLE andDelagate:nil];
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.arrayList count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentity = @"caogaologViewCell";
    CaogaologViewCell *cell = (CaogaologViewCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentity];
        if (cell ==  nil) {
            cell = [[CaogaologViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentity];
        }
    NSDictionary *dict=[self.arrayList objectAtIndex:indexPath.row];
    NSString *content=[dict objectForKey:@"str5"];
    if ([dict objectForKey:@"str6"]!=nil&&![[dict objectForKey:@"str6"] isEqual:@""]) {
            UIFont *font=[UIFont fontWithName:FONT_FAMILY size:15.0f];
            CGSize size=[Utility getStringSize:content font:font withMaxSize:CGSizeMake(240, 100)];

            cell.frame=CGRectMake(0, 0, 320, size.height+92);
            cell.content.hidden=NO;
            cell.yuyinButton.hidden=YES;
            cell.content.frame=CGRectMake(60, 62, 240, size.height);
            cell.content.numberOfLines=size.height/20;
            cell.content.text=content;
            
            
        }else{//图片85*58
            cell.frame=CGRectMake(0, 0, 320, 125);
            cell.content.hidden=YES;
            cell.yuyinButton.frame=CGRectMake(60, 62, 73, 33);
            cell.yuyinButton.hidden=NO;
            
        }
        if ([[dict objectForKey:@"str3"] isEqualToString:@""]) {
              cell.logo.image=[Utility imageNamed:@"pesonalLogo.png"];
        }else{
              [cell.logo setImageUrl:[NSString stringWithFormat:@"%@/%@",[Utility  getServerUrl],[dict objectForKey:@"str3"]]];
        }
        cell.nickName.text=[dict objectForKey:@"str2"];
       
        cell.detalContent.text=[NSString stringWithFormat:@"%@  %@供稿  阅读:%@", [Utility datefromSeconds:[dict objectForKey:@"dat1"] andDateFormat:@"yyyy-MM-dd"],[dict objectForKey:@"str2"],[dict objectForKey:@"int3"]];
        cell.detalContent2.frame=CGRectMake(60, cell.frame.size.height-25, 240, 15);
    cell.detalContent2.text=[NSString stringWithFormat:@"阅读：%@  收藏：%@  评论：%@  分享：%@  ",[dict objectForKey:@"int3"],[dict objectForKey:@"int4"],[dict objectForKey:@"int2"],[dict objectForKey:@"int1"]];
    return cell;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    UIView *view=[self.view  viewWithTag:FOOTVIEWTAG];
    if (!self.isEndData&&scrollView.contentOffset.y+self.mytableView.frame.size.height-view.frame.size.height>=self.mytableView.contentSize.height) {
        self.mytableView.contentInset=UIEdgeInsetsMake(0, 0, view.frame.size.height, 0);
        self.mytableView.tableFooterView.hidden=NO;
        if (self.arrayList.count>0) {
            [self loaddataFromserverWIthurrpage:self.currpage+1];
        }
        
    }
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
